package com.ucar.common.config;

import org.aopalliance.intercept.MethodInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.Pointcut;
import org.springframework.aop.PointcutAdvisor;
import org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

import com.ucar.common.annotation.ExeMethodTimeAnnotation;
import com.ucar.common.interceptor.ExeMethodTimeInterceptor;

/**
 * 
 * @author dinghao
 *
 */
@Configuration
@EnableAspectJAutoProxy
public class ExeMethodTimeConfig {
	
	// 日志
	private final Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 使用日志记录方法执行前后的耗时
	 * 
	 * @return
	 */
	@Bean("exeMethodTimeInterceptor")
	public PointcutAdvisor registerMethodConsumedTimeRecorder() {
		Pointcut pointcut = new AnnotationMatchingPointcut(null, ExeMethodTimeAnnotation.class);
		MethodInterceptor interceptor = new ExeMethodTimeInterceptor();
		DefaultBeanFactoryPointcutAdvisor pointcutAdvisor = new DefaultBeanFactoryPointcutAdvisor();
		pointcutAdvisor.setAdvice(interceptor);
		pointcutAdvisor.setPointcut(pointcut);
		pointcutAdvisor.setOrder(0);
		logger.info("成功实例化MethodConsumedTimeRecorder的切面");
		return pointcutAdvisor;
	}
}
